<template>
	<view class="account-container">
		<!-- 个人信息 -->
		<view class="profile-section">
			<view class="avatar-wrapper">
				<image class="avatar"
					:src="userInfo.avatar ? `http://localhost:8080${userInfo.avatar}` : '/static/dis-img1.jpg'"
					mode="aspectFill"></image>
			</view>
			<view class="info-wrapper">
				<text class="nickname">{{userInfo.nickname}}</text>


				<view class="member-level">
					<uni-icons type="star-filled" size="16" color="#FFD700"></uni-icons>
					<text>{{userInfo.level}}</text>
				</view>
			</view>
			<uni-icons class="more-icon" type="right" size="20" color="#999" @click="gotoperson"></uni-icons>
		</view>

		<!-- 订单状态 -->
		<view class="order-section">
			<view class="section-header">
				<view class="redstyle"></view>
				<text class="title">全部订单</text>
				<text class="more" @click="goToOrderList">查看全部 ></text>
			</view>
			<view class="order-status">
				<view class="status-item" v-for="item in orderStatus" :key="item.type"
					@click="goToOrderList(item.type)">
					<uni-icons :type="item.icon" size="28" :color="item.color"></uni-icons>
					<text class="status-text">{{item.text}}</text>
					<text class="badge" v-if="item.count > 0">{{item.count}}</text>
				</view>
			</view>
		</view>

		<!-- 我的钱包 -->
		<view class="wallet-section">
			<view class="section-header">
				<view class="redstyle"></view>
				<text class="title">我的钱包</text>
			</view>
			<view class="wallet-grid">
				<view class="wallet-item" v-for="item in walletItems" :key="item.type" @click="handleWallet(item.type)">
					<text class="amount">{{item.amount}}</text>
					<text class="label">{{item.label}}</text>
					<text class="badge" v-if="item.count > 0">{{item.count}}</text>
				</view>
			</view>
		</view>

		<!-- 功能区域 -->
		<view class="function-section">
			<view class="function-grid">
				<view class="function-item" v-for="item in functions" :key="item.type" @click="handleFunction(item)">
					<uni-icons :type="item.icon" size="28" :color="item.color"></uni-icons>
					<text class="function-text">{{item.text}}</text>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				userInfo: {
					avatar: '', // 初始化为空
					nickname: '', // 初始化为空
					level: '黄金会员' // 固定值
				},
				orderStatus: [{
						type: 'unpaid',
						icon: 'wallet',
						text: '待支付',
						count: 0,
						color: '#FFA500'
					},
					{
						type: 'unreceived',
						icon: 'cart',
						text: '待收货',
						count: 1,
						color: '#67C23A'
					},
					{
						type: 'unevaluated',
						icon: 'compose',
						text: '待评价',
						count: 0,
						color: '#409EFF'
					},
					{
						type: 'after-sale',
						icon: 'refreshempty',
						text: '退换/售后',
						count: 0,
						color: '#F56C6C'
					}
				],
				walletItems: [{
						type: 'balance',
						label: '账户现金',
						amount: '¥0.00',
						count: 0
					},
					{
						type: 'cashback',
						label: '账户返现',
						amount: '¥0.00',
						count: 0
					},
					{
						type: 'points',
						label: '账户积分',
						amount: '1000',
						count: 0
					},
					{
						type: 'coupons',
						label: '优惠券',
						amount: '3张',
						count: 0
					}
				],
				functions: [{
						type: 'gear',
						icon: 'gear',
						text: '设置中心',
						color: '#333',
						path: '/pages/individual-account/individual-gear' // 设置中心路径
					},
				
					{
						type: 'service',
						icon: 'headphones',
						text: '帮助中心',
						color: '#67C23A',
						path: '/pages/individual-account/individual-customer-service' // 帮助中心路径
					},
					{
						type: 'vip',
						icon: 'vip',
						text: '会员中心',
						color: '#409EFF',
						path: "/pages/individual-account/individual-member-center", // 会员中心路径
					},
					{
						type: 'notification',
						icon: 'notification',
						text: '消息通知',
						color: '#FFA500',
						path: '/pages/individual-account/individual-notifications' // 消息通知路径
					},
					{
						type: 'password',
						icon: 'locked',
						text: '找回密码',
						color: '#F56C6C'
					},
					{
						type: 'logout',
						icon: 'close',
						text: '退出登录',
						color: '#999'
					}
				]
			};
		},
		onLoad() {
			this.refreshData();
		},
		onShow() {
			// 每次页面显示时刷新数据
			this.refreshData();
		},
		methods: {
			// 封装数据刷新方法
			refreshData() {
				this.fetchUserProfile();
				// 如果有其他数据需要刷新，可以在这里调用
			},

			// 方法中统一处理页面跳转
			handleFunction(item) {
				console.log('完整功能项:', item);
			 if (item.type === 'logout') {
					this.logout();
				} else {
					// 添加路径校验
					if (!item.path) {
						uni.showToast({
							title: '功能暂未开放',
							icon: 'none'
						});
						return;
					}
					uni.navigateTo({
						url: item.path
					});
				}
			},
			// 新增方法：获取用户信息
			async fetchUserProfile() {
				try {
					const res = await uni.request({
						url: 'http://localhost:8080/system/user/profile',
						header: {
							'Authorization': uni.getStorageSync('token')
						}
					});

					if (res.data.code === 200) {
						const profile = res.data.data;
						this.userInfo = {
							avatar: profile.avatar, // 根据实际接口字段调整
							nickname: profile.nickName, // 根据实际接口字段调整
							level: '黄金会员' // 保持固定
						};
					}
				} catch (error) {
					console.error('获取用户信息失败:', error);
					uni.showToast({
						title: '数据加载失败',
						icon: 'none'
					});
				}
			},

			goToOrderList(type) {
				uni.navigateTo({
					url: `/pages/view-all-orders/view-all-orders`
				});
			},
			handleWallet(type) {
				uni.navigateTo({
					url: `/pages/wallet/${type}`
				});
			},
			// handleFunction(type) {
			// 	if (type === 'logout') {
			// 		this.logout();
			// 	} else {
			// 		uni.navigateTo({
			// 			url: `/pages/${type}/index`
			// 		});
			// 	}
			// },
			gotoperson() {
				uni.navigateTo({
					url: '/pages/detailed-information/detailed-information'
				});
			},
			logout() {
				uni.showModal({
					title: '提示',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							uni.request({
								url: 'http://localhost:8080/logout',
								method: 'POST',
								header: {
									'Authorization': uni.getStorageSync('token')
								},
								success: (res) => {
									if (res.statusCode === 200 && res.data.code === 200) {
										try {
											uni.removeStorageSync('token');
											uni.showToast({
												title: '退出成功',
												icon: 'success',
												duration: 2000
											});
											setTimeout(() => {
												uni.reLaunch({
													url: '/pages/login/login'
												});
											}, 2000);
										} catch (e) {
											console.error('清除 token 失败', e);
											uni.showToast({
												title: '退出失败',
												icon: 'error',
												duration: 2000
											});
										}
									} else {
										uni.showToast({
											title: '退出失败',
											icon: 'error',
											duration: 2000
										});
									}
								},
								fail: (err) => {
									console.error('退出接口调用失败', err);
									uni.showToast({
										title: '退出失败',
										icon: 'error',
										duration: 2000
									});
								}
							});
						}
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.account-container {
		padding: 20rpx;
		// background: #f5f5f5;
		background: #FFF8F0;
	}

	.profile-section {
		display: flex;
		align-items: center;
		padding: 40rpx;
		background: #fff;
		border-radius: 16rpx;
		margin-bottom: 20rpx;

		.avatar-wrapper {
			width: 120rpx;
			height: 120rpx;
			border-radius: 50%;
			overflow: hidden;
			margin-right: 30rpx;

			.avatar {
				width: 100%;
				height: 100%;
			}
		}

		.info-wrapper {
			flex: 1;

			.nickname {
				font-size: 36rpx;
				font-weight: bold;
				color: #333;
				display: block;
				margin-bottom: 10rpx;
			}

			.member-level {
				display: flex;
				align-items: center;
				font-size: 26rpx;
				color: #FFA500;

				text {
					margin-left: 8rpx;
				}
			}
		}

		.more-icon {
			width: 60rpx;
			text-align: right;
		}
	}
	.redstyle{
		    height: 18px;
		    width: 5px;
		    background: red;
		    margin-right: 5px;
		    border-radius: 10px;
	}
	.section-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx 0;
		    border-bottom: 1px solid #eee;
		
		.title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
			flex: 1;
		}

		.more {
			font-size: 26rpx;
			color: #999;
		}
	}

	.order-section,
	.wallet-section {
		background: #fff;
		border-radius: 16rpx;
		padding: 0 30rpx;
		margin-bottom: 20rpx;
	}

	.order-status {
		display: flex;
		justify-content: space-around;
		padding: 30rpx 0;

		.status-item {
			position: relative;
			text-align: center;

			.status-text {
				display: block;
				font-size: 26rpx;
				color: #666;
				margin-top: 10rpx;
			}

			.badge {
				position: absolute;
				top: -10rpx;
				right: -10rpx;
				background: #F56C6C;
				color: #fff;
				font-size: 20rpx;
				padding: 2rpx 10rpx;
				border-radius: 20rpx;
			}
		}
	}

	.wallet-grid {
		display: flex;
		flex-wrap: wrap;
		padding: 20rpx 0;

		.wallet-item {
			width: 50%;
			padding: 30rpx 0;
			text-align: center;
			position: relative;

			.amount {
				font-size: 36rpx;
				color: #FFA500;
				display: block;
				margin-bottom: 10rpx;
			}

			.label {
				font-size: 26rpx;
				color: #666;
			}

			.badge {
				position: absolute;
				top: 20rpx;
				right: 40rpx;
				background: #F56C6C;
				color: #fff;
				font-size: 20rpx;
				padding: 2rpx 10rpx;
				border-radius: 20rpx;
			}
		}
	}

	.function-section {
		background: #fff;
		border-radius: 16rpx;
		padding: 20rpx;

		.function-grid {
			display: flex;
			flex-wrap: wrap;

			.function-item {
				width: 25%;
				padding: 30rpx 0;
				text-align: center;

				.function-text {
					display: block;
					font-size: 26rpx;
					color: #666;
					margin-top: 10rpx;
				}
			}
		}
	}
</style>
